package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.conn;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class orders_Add extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public orders_Add() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	//添加订单信息
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		PrintWriter out = response.getWriter();

		conn cn = new conn();

		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");

		String no = df.format(new Date()).toString();
        String uid=request.getParameter("uid");
        String username=request.getParameter("username");
        String phone=request.getParameter("phone");
        String address=request.getParameter("address");
        String ormemo=request.getParameter("memo");
        String smoney=request.getParameter("omoney");

        String flag=request.getParameter("f");

		if (flag.equals("0")) {


			String sql="insert into orders(no,uid,username,phone,address,ormemo,smoney,oflag,otime,flag)" +
					" values('"+no+"',"+uid+",'"+username+"','"+phone+"','"+address+"','"+ormemo+"',"+smoney+",'等待发货',now(),"+flag+")";
			


			cn.update(sql);


			//遍历
			String jsonString =request.getParameter("cartItems");

			out.println(jsonString);

			JSONArray jsonArray1 = JSONArray.fromObject(jsonString);
			for (int i = 0; i < jsonArray1.size(); i++) {
				JSONObject jb = jsonArray1.getJSONObject(i);

				cn.update("insert into ordermore(no, pid, price,quantity) " +
						" VALUES ('"+no+"',"+jb.getString("id")+","+jb.getString("price")+","+jb.getString("value")+")");
			}

		} else {


			String sql="insert into orders(no,uid,username,phone,address,ormemo,smoney,oflag,otime,flag)" +
					" values('"+no+"',"+uid+",'"+username+"','"+phone+"','"+address+"','"+ormemo+"',"+smoney+",'等待发货',now(),"+flag+")";
			


			cn.update(sql);


			//遍历
			String jsonString = request.getParameter("cartItems");
			JSONArray jsonArray1 = JSONArray.fromObject(jsonString);
			for (int i = 0; i < jsonArray1.size(); i++) {
				JSONObject jb = jsonArray1.getJSONObject(i);
				JSONArray jsonArray2 = JSONArray.fromObject(jb.getString("more"));
				for (int j=0;j<jsonArray2.size();j++)
				{
					JSONObject jb2 = jsonArray2.getJSONObject(j);

					if(Integer.valueOf(jb2.getString("bqu"))>0)
					{
						cn.update("insert into ordermore(no, pid, quantity, price)" +
								" values('"+no+"',"+jb2.getString("pid")+","+jb2.getString("bqu")+","+jb2.getString("price")+") ");

					}


				}



			}

		}
		


		cn.update("insert into orderstatus(no, omemo, otime) VALUES('"+no+"','提交订单',now()) ");

		JSONArray jsonarray = new JSONArray();
		JSONObject jsonobj = new JSONObject();
		jsonobj.put("jieguo", 1);
		jsonarray.add(jsonobj);
		out = response.getWriter();
		out.println(jsonarray);


	
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

